package com.gnerv.battle.boot.project.service.impl;

import com.gnerv.battle.boot.project.entity.MonitorDatasource;
import com.gnerv.battle.boot.project.dao.MonitorDatasourceDao;
import com.gnerv.battle.boot.project.service.MonitorDatasourceService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 监控数据源表(MonitorDatasource)表服务实现类
 *
 * @author makejava
 * @since 2024-05-26 11:22:01
 */
@Service("monitorDatasourceService")
public class MonitorDatasourceServiceImpl implements MonitorDatasourceService {
    @Resource
    private MonitorDatasourceDao monitorDatasourceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MonitorDatasource queryById(Long id) {
        return this.monitorDatasourceDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param monitorDatasource 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<MonitorDatasource> queryByPage(MonitorDatasource monitorDatasource, PageRequest pageRequest) {
        long total = this.monitorDatasourceDao.count(monitorDatasource);
        return new PageImpl<>(this.monitorDatasourceDao.queryAllByLimit(monitorDatasource, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param monitorDatasource 实例对象
     * @return 实例对象
     */
    @Override
    public MonitorDatasource insert(MonitorDatasource monitorDatasource) {
        this.monitorDatasourceDao.insert(monitorDatasource);
        return monitorDatasource;
    }

    /**
     * 修改数据
     *
     * @param monitorDatasource 实例对象
     * @return 实例对象
     */
    @Override
    public MonitorDatasource update(MonitorDatasource monitorDatasource) {
        this.monitorDatasourceDao.update(monitorDatasource);
        return this.queryById(monitorDatasource.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.monitorDatasourceDao.deleteById(id) > 0;
    }
}
